/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.freemarker;

import com.nanosn.coffeemaker.Application;
import com.nanosn.utils.Jar;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
 * A class to interface with FreeMarker.
 * @author selkhateeb
 */
public class FreeMarker
{

    /**
     *
     * @param templateFileName
     * @param destinationFileName
     * @param dataMap 
     * @throws java.io.IOException
     * @throws TemplateException
     */
    public static void ProcessSingleTemplateFile(String templateFileName,
                                                 String destinationFileName,
                                                 Map dataMap)
            throws IOException, TemplateException
    {
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(Application.TMP));


        Template template = cfg.getTemplate(templateFileName);

        File directories = new File(destinationFileName).getParentFile();
        System.out.println("Making dirs: " + directories.getAbsolutePath());
        directories.mkdirs();

        Writer writer = null;
        try
        {
            writer = new FileWriter(destinationFileName);
            template.process(dataMap, writer);
            writer.flush();
        }
        catch (IOException iOException)
        {
            throw iOException;
        }
        catch (TemplateException templateException)
        {
            throw templateException;
        }
        finally
        {
            if(writer != null)
            {
                writer.close();
            }
        }
    }

    public static void ProcessTemplateFileLocatedInJarFile(
            String jarFile,
            String templateFilePath,
            String destinationFileName,
            Map dataMap) throws IOException, TemplateException
    {

        Jar.ExtractSingleFile(jarFile, templateFilePath, Application.TMP);

        ProcessTemplateFileFromTemporaryDirectory(
                templateFilePath,
                destinationFileName, dataMap);
    }

    public static void ProcessTemplateFileLocatedInJarFile(
            File jarFile,
            String templateFilePath,
            String destinationFileName,
            Map dataMap) throws IOException, TemplateException
    {

        Jar.ExtractSingleFile(jarFile, templateFilePath, Application.TMP);

        ProcessTemplateFileFromTemporaryDirectory(
                templateFilePath,
                destinationFileName, dataMap);
    }

    private static void ProcessTemplateFileFromTemporaryDirectory(
            String templateFilePath,
            String destinationFileName,
            Map dataMap)
            throws TemplateException, IOException
    {
        /** We are only interested in the template file name since the
         * ExtractSingleFile extracts only the file name and does not create
         * The parent directories of the template file
         */
        String templateFileName =
                templateFilePath.substring(
                templateFilePath.lastIndexOf(File.separatorChar));

        ProcessSingleTemplateFile(templateFileName,
                                  destinationFileName,
                                  dataMap);
    }
}
